<%-- 
    Document   : queryWeb
    Created on : 08/04/2008, 13:48:45
    Author     : Tom Hayden
--%>

<%@page contentType="text/html" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%--@ taglib uri="http://displaytag.sf.net" prefix="display" --%>

<c:if test="${not empty param.query and not empty param.tipoStatement}">
    <c:catch var="exception">
        <c:if test="${param.tipoStatement == 'consulta'}">
            <sql:query var="resultSet" dataSource="jdbc/PetstoreDS">${param.query}</sql:query>
        </c:if>
        <c:if test="${param.tipoStatement == 'update'}">
            <sql:update var="rowsAffected" dataSource="jdbc/PetstoreDS">${param.query}</sql:update>
        </c:if>
    </c:catch>
</c:if>
<c:if test="${not empty exception}">
    <div class="msgErro">
        <b><fmt:message key="errors.validation"/></b>
        <br>&#0187; ${exception.message}
    </div>
</c:if>


<h1><fmt:message key="queryweb.title"/></h1>
<form name="queryWebForm" action="queryWeb.do" method="post">
    <table class="form">
        <tbody>
            <tr>
                <th><fmt:message key="queryweb.field.query"/>:</th>
                <td><textarea name="query" rows="10" cols="60"><c:out value="${param.query}" default="SELECT getdate() AS today"/></textarea></td>
            </tr>
            <tr>
                <th><fmt:message key="queryweb.field.tipoStatement"/>:</th>
                <td><input type="radio" class="element" name="tipoStatement" value="consulta" <c:if test="${param.tipoStatement != 'update'}">checked</c:if>><fmt:message key="queryweb.field.tipoStatement.consulta"/>
                    <input type="radio" class="element" name="tipoStatement" value="update" <c:if test="${param.tipoStatement == 'update'}">checked</c:if>><fmt:message key="queryweb.field.tipoStatement.update"/></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td align="left">
                    <input type="submit" value="<fmt:message key='global.label.botaosubmit'/>" />
                    <input type="button" value="<fmt:message key='global.label.botaovoltar'/>" onclick="goURL('home.do');">
                </td>
            </tr>
        </tbody>
    </table>
</form>
<c:if test="${not empty resultSet}">
    <div style="overflow: scroll; width: 750px; height: 300px; padding: 0.5em; margin: 0.5em;">
        <%--<display:table name="${resultSet.rows}" class="its"/>--%>
        <table border="1" class="grid" style="font-size: 10px;">
            <tr>
                <c:forEach var="colTitle" items="${resultSet.columnNames}">
                    <th bgcolor="#EAF4F7">${colTitle}</th>
                </c:forEach>
            </tr>
            <c:forEach var="row" items="${resultSet.rowsByIndex}" varStatus="idx">
                <tr bgcolor="<c:if test="${idx.count mod 2 == 0}">#EAF4F7</c:if>">
                    <c:forEach var="colValue" items="${row}">
                        <td>${colValue}</td>
                    </c:forEach>
                </tr>
            </c:forEach>
        </table>
    </div>
    ${resultSet.rowCount} registros encontrados.
</c:if>
<c:if test="${not empty rowsAffected}">
    ${rowsAffected} <fmt:message key="queryweb.message.rowsAffected"/>
</c:if>